package com.taxshare.seller.service.sst;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.taxshare.seller.constant.CodeConstant;
import com.taxshare.seller.entity.EquipmentEntity;
import com.taxshare.seller.entity.SellerInvoiceDetailEntity;
import com.taxshare.seller.entity.SellerInvoiceEntity;
import com.taxshare.seller.view.dto.SynchronizeInvoiceDTO;
import com.taxshare.tools.util.DateUtil;
import com.taxshare.tools.util.StringUtil;
import com.taxshare.tools.util.UUIDUitl;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.apache.bcel.classfile.Code;
import org.checkerframework.checker.units.qual.K;

import java.util.*;
import java.util.stream.Collectors;

@Slf4j
public class IntJsonUtil {

    private IntJsonUtil() {

    }

    static final String TRUE = "true";
    static final String FPLXDM = "fplxdm";

    /**
     * 应用登记
     *
     * @param devKey
     * @param appName
     * @return
     */
    public static String appRegister(String devKey, String appName) {

        JSONObject json = new JSONObject();
        json.put("serviceid", "DJAPP");
        json.put("dev_key", devKey);
        json.put("app_name", appName);
        return json.toString();
    }

    /**
     * 通用头
     *
     * @param serviceId
     * @param nsrsbh
     * @param async
     * @param appid
     * @param sid
     * @param inpuMap
     * @return
     */
    public static String getJson(String serviceId, String nsrsbh, String async, String appid, String sid, Map inpuMap) {
        JSONObject json = new JSONObject();
        json.put("serviceid", serviceId);
        json.put("nsrsbh", nsrsbh);
        json.put("async", async);
        json.put("appid", appid);
        json.put("sid", sid);
        json.put("input", inpuMap);
        return json.toString();
    }

    /**
     * 发票开具
     *
     * @param appid
     * @param mode
     * @param sellerInvoiceEntity
     * @param sellerInvoiceDetailEntities
     * @return
     */
    public static String makeInvoice(String appid, String mode, SellerInvoiceEntity sellerInvoiceEntity, List<SellerInvoiceDetailEntity> sellerInvoiceDetailEntities) {
        Map map = new HashMap<>();
        Map<String, List<SellerInvoiceDetailEntity>> collect = sellerInvoiceDetailEntities.stream().collect(Collectors.groupingBy(SellerInvoiceDetailEntity::getTaxRate));
        String makeInvoiceType = sellerInvoiceEntity.getMakeInvoiceType();
        String fplxdm = checkFplxdm(mode, sellerInvoiceEntity.getInvoiceType());
        map.put(FPLXDM, fplxdm);
        map.put("kplx", sellerInvoiceEntity.getMakeInvoiceType());
        String tspz = getTspz(mode, sellerInvoiceEntity.getInvoiceType());
        map.put("tspz", tspz);
        map.put("xhdwdzdh", sellerInvoiceEntity.getSellerAddressTel());
        map.put("xhdwyhzh", sellerInvoiceEntity.getSellerBankAccount());
        map.put("ghdwsbh", sellerInvoiceEntity.getPurchaserTaxNo());
        map.put("ghdwmc", sellerInvoiceEntity.getPurchaserName());
        map.put("ghdwdzdh", sellerInvoiceEntity.getPurchaserAddressTel());
        map.put("ghdwyhzh", sellerInvoiceEntity.getPurchaserBankAccount());
        List<Map> detailList = new ArrayList<>();
        String zyspmc = "";
        String zhsl = "";
        String sl = "";
        String spmc = "";
        int detailSize = sellerInvoiceDetailEntities.size();
        if (detailSize >= 8 && CodeConstant.COMM_CODE_1.equals(makeInvoiceType)) {
            // 负数清单发票红字开具
            if (CodeConstant.SKP.equals(mode) || CodeConstant.UKEY.equals(mode)) {
                // 根据税率分组，区分多税率和单税率传参
                if (collect.size() > 1) {
                    zhsl = "99.01";
                } else {
                    zhsl = sellerInvoiceEntity.getTaxRate();
                    sl = sellerInvoiceEntity.getTaxRate();
                }
                spmc = "（详见销货清单）";
            } else {
                if (collect.size() > 1) {
                    sl = "";
                    zhsl = "";
                } else {
                    zhsl = sellerInvoiceEntity.getTaxRate();
                    sl = sellerInvoiceEntity.getTaxRate();
                }
                spmc = "详见对应正数发票及清单";
            }
            zyspmc = "（详见销货清单）";
            Map detailMap = new HashMap();
            detailMap.put("xh", "");
            detailMap.put("fphxz", CodeConstant.COMM_CODE_0);
            detailMap.put("spmc", spmc);
            detailMap.put("ggxh", "");
            detailMap.put("dw", "");
            detailMap.put("spsl", "");
            detailMap.put("dj", "");
            detailMap.put("je", sellerInvoiceEntity.getTotalSum());
            detailMap.put("sl",sl);
            detailMap.put("se", sellerInvoiceEntity.getTotalTax());
            detailMap.put("hsbz", "0");
            detailMap.put("spbm", "");
            detailMap.put("zxbm", "");
            detailMap.put("yhzcbs", CodeConstant.COMM_CODE_0);
            detailMap.put("slbs", "");
            detailMap.put("zzstsgl", "");
            detailList.add(detailMap);
        } else {
            for (int i = 0; i < sellerInvoiceDetailEntities.size(); i++) {
                Map detailMap = new HashMap();
                if (CodeConstant.SKP.equals(mode) || CodeConstant.UKEY.equals(mode)) {
                    // 根据税率分组，区分多税率和单税率传参
                    if (collect.size() > 1) {
                        zhsl = "";
                    } else {
                        zhsl = sellerInvoiceEntity.getTaxRate();
                    }
                } else {
                    if (collect.size() > 1) {
                        zhsl = "";
                    } else {
                        zhsl = sellerInvoiceEntity.getTaxRate();
                    }
                }
                SellerInvoiceDetailEntity invoiceDetailEntity = sellerInvoiceDetailEntities.get(i);
                if (i == 0) {
                    zyspmc = invoiceDetailEntity.getGoodsName();
                }
                detailMap.put("xh", invoiceDetailEntity.getInvoiceSort());
                detailMap.put("fphxz", invoiceDetailEntity.getInvoiceRowNature());
                detailMap.put("spmc", invoiceDetailEntity.getGoodsName());
                detailMap.put("ggxh", StringUtil.validator(invoiceDetailEntity.getModel()));
                detailMap.put("dw", StringUtil.validator(invoiceDetailEntity.getUnit()));
                detailMap.put("spsl", StringUtil.validator(invoiceDetailEntity.getGoodsNumber()));
                detailMap.put("dj", StringUtil.validator(invoiceDetailEntity.getUnitPrice()));
                detailMap.put("je", StringUtil.validator(invoiceDetailEntity.getGoodsSum()));
                detailMap.put("sl", StringUtil.validator(invoiceDetailEntity.getTaxRate()));
                detailMap.put("se", StringUtil.validator(invoiceDetailEntity.getGoodTax()));
                detailMap.put("hsbz", "0");
                detailMap.put("spbm", StringUtil.validator(invoiceDetailEntity.getTaxCode()));
                detailMap.put("zxbm", "");
                //优惠政策标识（0无优惠政策 1有优惠政策）
                String preferentialMark = null;
                //优惠政策名称
                String preferentialName = null;
                String taxRateMark = StringUtil.validator(invoiceDetailEntity.getTaxRateMark());
                if (CodeConstant.COMM_CODE_1.equals(taxRateMark)) {
                    preferentialMark = CodeConstant.COMM_CODE_1;
                    preferentialName = "免税";
                } else if (CodeConstant.COMM_CODE_2.equals(taxRateMark)) {
                    preferentialMark = CodeConstant.COMM_CODE_1;
                    preferentialName = "不征税";
                } else if (CodeConstant.COMM_CODE_3.equals(taxRateMark)) {
                    preferentialMark = CodeConstant.COMM_CODE_0;
                    preferentialName = "";
                } else {
                    preferentialMark = CodeConstant.COMM_CODE_0;
                }
                detailMap.put("yhzcbs", preferentialMark);
                detailMap.put("slbs", taxRateMark);
                detailMap.put("zzstsgl", preferentialName);
                detailList.add(detailMap);
            }
        }
        map.put("mx", detailList);
        map.put("hjje", sellerInvoiceEntity.getTotalSum());
        map.put("hjse", sellerInvoiceEntity.getTotalTax());
        map.put("jshj", sellerInvoiceEntity.getTotalSumTax());
        map.put("zhsl",zhsl);
        map.put("bz", sellerInvoiceEntity.getRemark());
        map.put("skr", sellerInvoiceEntity.getPayee());
        map.put("fhr", sellerInvoiceEntity.getReviewer());
        map.put("kpr", sellerInvoiceEntity.getDrawer());
        map.put("tzdbh", StringUtil.validator(sellerInvoiceEntity.getRedInfoNo()));
        map.put("yfpdm", StringUtil.validator(sellerInvoiceEntity.getOldInvoiceCode()));
        map.put("yfphm", StringUtil.validator(sellerInvoiceEntity.getOldInvoiceNo()));
        map.put("zyspmc", zyspmc);
        map.put("gmf_dzyx", StringUtil.validator(sellerInvoiceEntity.getMailBox()));
        map.put("gmf_sjhm", StringUtil.validator(sellerInvoiceEntity.getPhone()));
        map.put("gmf_openid", "");
        return getJson("FPKJ", sellerInvoiceEntity.getSellerTaxNo(), TRUE, appid,
                sellerInvoiceEntity.getSerialNo(), map);
    }

    /**
     * 发票开具
     *
     * @param appid               appid
     * @param mode                ode
     * @param sellerInvoiceEntity 发票信息
     * @param detailEntity        发票商品
     * @return 结果
     */
    public static String carMakeInvoice(String appid, String mode, SellerInvoiceEntity sellerInvoiceEntity, SellerInvoiceDetailEntity detailEntity) {
        Map map = new HashMap<>();
        String fplxdm = checkFplxdm(mode, sellerInvoiceEntity.getInvoiceType());
        map.put(FPLXDM, fplxdm);
        map.put("kplx", sellerInvoiceEntity.getMakeInvoiceType());
        map.put("ghdwsbh", sellerInvoiceEntity.getPurchaserTaxNo());
        map.put("ghdwmc", sellerInvoiceEntity.getPurchaserName());
        map.put("ghdwdzdh", sellerInvoiceEntity.getPurchaserAddressTel());
        map.put("ghdwyhzh", sellerInvoiceEntity.getPurchaserBankAccount());
        map.put("sfzh", sellerInvoiceEntity.getPurchaserTaxNo());
        map.put("zzssl", sellerInvoiceEntity.getTaxRate());
        map.put("scqymc", detailEntity.getProductionCompanyName());
        map.put("cllx", detailEntity.getCarCategoryName());
        map.put("cpxh", detailEntity.getBrandMode());
        map.put("cd", detailEntity.getProductionPlaceName());
        map.put("hezh", detailEntity.getCertificateNum());
        map.put("jkzmsh", detailEntity.getImportTestimonialNum());
        map.put("sjdh", detailEntity.getInspectionOdd());
        map.put("fdjhm", detailEntity.getEngineNum());
        map.put("dw", detailEntity.getTonnage());
        map.put("cjhm", detailEntity.getCarDiscernCode());
        map.put("jshj", sellerInvoiceEntity.getTotalSumTax());
        map.put("dh", sellerInvoiceEntity.getSellerPhone());
        map.put("dz", sellerInvoiceEntity.getSellerAddress());
        map.put("zh", sellerInvoiceEntity.getSellerBankAccount());
        map.put("khyh", sellerInvoiceEntity.getSellerDepositBank());
        map.put("xcrs", detailEntity.getMaximumPeople());
        map.put("spbm", detailEntity.getTaxCode());
        map.put("kpr", sellerInvoiceEntity.getDrawer());
        map.put("tzdbh", StringUtil.validator(sellerInvoiceEntity.getRedInfoNo()));
        map.put("yfpdm", StringUtil.validator(sellerInvoiceEntity.getOldInvoiceCode()));
        map.put("yfphm", StringUtil.validator(sellerInvoiceEntity.getOldInvoiceNo()));
        String ykprq = sellerInvoiceEntity.getInvoiceDate();
        if (StrUtil.isNotEmpty(ykprq)) {
            ykprq = ykprq.replace("-", "");
        }
        map.put("ykprq", ykprq);
        return getJson("FPKJ", sellerInvoiceEntity.getSellerTaxNo(), TRUE, appid,
                sellerInvoiceEntity.getSerialNo(), map);
    }


    /**
     * 发票作废
     *
     * @param mode
     * @param appid
     * @param sellerInvoiceEntity
     * @return
     */
    public static String cancelInvoice(String mode, String appid, SellerInvoiceEntity sellerInvoiceEntity) {
        Map map = new HashMap();
        String fplxdm = checkFplxdm(mode, sellerInvoiceEntity.getInvoiceType());
        map.put(FPLXDM, fplxdm);
        map.put("fpdm", sellerInvoiceEntity.getInvoiceCode());
        map.put("fphm", sellerInvoiceEntity.getInvoiceNo());
        map.put("hjje", sellerInvoiceEntity.getTotalSum());
        map.put("zfr", sellerInvoiceEntity.getDrawer());
        return getJson("FPZF", sellerInvoiceEntity.getSellerTaxNo(), TRUE, appid, UUIDUitl.UUID32(), map);

    }

    /**
     * 发票打印
     *
     * @param mode
     * @param appid
     * @param pringtType
     * @param sellerInvoiceEntity
     * @return
     */

    public static String printInvoice(String mode, String appid, String pringtType, SellerInvoiceEntity sellerInvoiceEntity) {
        Map map = new HashMap();
        String fplxdm = checkFplxdm(mode, sellerInvoiceEntity.getInvoiceType());
        map.put(FPLXDM, fplxdm);
        map.put("fpdm", sellerInvoiceEntity.getInvoiceCode());
        map.put("fphm", sellerInvoiceEntity.getInvoiceNo());
        map.put("dylx", pringtType);
        map.put("dyjmc", "");
        return getJson("FPDY", sellerInvoiceEntity.getSellerTaxNo(), TRUE, appid, UUIDUitl.UUID32(), map);
    }


    /**
     * 获取未使用发票代码号码
     *
     * @param mode
     * @param appid
     * @param sellerInvoiceEntity
     * @return
     */
    public static String getUserInvoiceNo(String mode, String appid, SellerInvoiceEntity sellerInvoiceEntity) {
        Map map = new HashMap();
        String fplxdm = checkFplxdm(mode, sellerInvoiceEntity.getInvoiceType());
        map.put(FPLXDM, fplxdm);
        return getJson("HQDQFPDMHM", sellerInvoiceEntity.getSellerTaxNo(), TRUE, appid, UUIDUitl.UUID32(), map);
    }

    /**
     * 红字通知单上传
     *
     * @param mode
     * @param appid
     * @param panNo
     * @param sellerInvoiceEntity
     * @param sellerInvoiceDetailEntities
     * @return
     */
    public static String redInvoiceInfoUpload(String mode, String appid, String panNo, SellerInvoiceEntity sellerInvoiceEntity, List<SellerInvoiceDetailEntity> sellerInvoiceDetailEntities) {
        Map map = new HashMap();
        String fplxdm = checkFplxdm(mode, sellerInvoiceEntity.getInvoiceType());
        map.put(FPLXDM, fplxdm);
        Map invMap = new HashMap();
        invMap.put("ReqNsrsbh", sellerInvoiceEntity.getSellerTaxNo());
        invMap.put("BillType", "0");
        invMap.put("Sbbh", panNo);
        invMap.put("TypeCode", sellerInvoiceEntity.getInvoiceCode());
        invMap.put("InvNo", sellerInvoiceEntity.getInvoiceNo());
        String kprq = getKprq(sellerInvoiceEntity.getInvoiceNo());
        invMap.put("Kprq", kprq);
        invMap.put("Szlb", "1");
        invMap.put("IsMutiRate", "0");
        String currentDate = DateUtil.formatDateToString6(new Date());
        invMap.put("Date", currentDate);
        invMap.put("BuyerName", sellerInvoiceEntity.getPurchaserName());
        invMap.put("BuyTaxCode", sellerInvoiceEntity.getPurchaserTaxNo());
        invMap.put("SellerName", sellerInvoiceEntity.getSellerName());
        invMap.put("SellTaxCode", sellerInvoiceEntity.getSellerTaxNo());
        invMap.put("Amount", sellerInvoiceEntity.getTotalSum());
        invMap.put("Tax", sellerInvoiceEntity.getTotalTax());
        invMap.put("ReqMemo", "00000001000");
        invMap.put("SPBMBBH", "33.0");
        invMap.put("SLBZ", "0");
        List detailList = new ArrayList();
        String taxRate = "";
        for (int i = 0; i < sellerInvoiceDetailEntities.size(); i++) {
            SellerInvoiceDetailEntity invoiceDetailEntity = sellerInvoiceDetailEntities.get(i);
            Map detailMap = new HashMap();
            taxRate = invoiceDetailEntity.getTaxRate();
            detailMap.put("GoodsName", invoiceDetailEntity.getGoodsName());
            detailMap.put("GoodsPrice", StringUtil.validator(invoiceDetailEntity.getUnitPrice()));
            detailMap.put("GoodsTaxRate", StringUtil.validator(invoiceDetailEntity.getTaxRate()));
            detailMap.put("GoodsNum", StringUtil.validator(invoiceDetailEntity.getGoodsNumber()));
            detailMap.put("GoodsJE", StringUtil.validator(invoiceDetailEntity.getGoodsSum()));
            detailMap.put("GoodsSE", StringUtil.validator(invoiceDetailEntity.getGoodTax()));
            detailMap.put("HS_BZ", "0");
            detailMap.put("SPBM", StringUtil.validator(invoiceDetailEntity.getTaxCode()));
            detailMap.put("SYYHZCBZ", StringUtil.validator(invoiceDetailEntity.getPreferentialMark()));
            detailMap.put("YHZC", StringUtil.validator(invoiceDetailEntity.getPreferentialName()));
            detailMap.put("LSLBZ", StringUtil.validator(invoiceDetailEntity.getTaxRateMark()));
            detailList.add(detailMap);
        }
        invMap.put("TaxRate", taxRate);
        invMap.put("RedInvReqBillMx", detailList);
        map.put("RedInvReqBill", invMap);
        return getJson("HZTZDSC", sellerInvoiceEntity.getSellerTaxNo(), TRUE, appid, UUIDUitl.UUID32(), map);
    }


    /**
     * 红字通知单下载
     *
     * @param appid
     * @param xxbbh
     * @param sqq
     * @param sqz
     * @param sbh
     * @return
     */
    public static String downLoadRedInfo(String appid, String xxbbh, String sqq, String sqz, String sbh) {
        Map map = new HashMap();
        map.put("xxbbh", xxbbh);
        map.put("sqqsrq", sqq);
        map.put("sqzzrq", sqz);
        return getJson("HZTZDXZ", sbh, TRUE, appid, UUIDUitl.UUID32(), map);
    }

    public static String syncInvoice(String mode, String appid, String dateS, String dateE, String nsrsbh, String invoiceType,String invoiceDate) {
        Map map = new HashMap();
        if (mode.equals("0")) {
            String year = invoiceDate.substring(0, 4);
            String month = invoiceDate.substring(4, 6);
            map.put(FPLXDM,checkFplxdm(mode,invoiceType));
            map.put("cxfs", "1");
            map.put("year", year);
            map.put("month", month);
        } else {
            map.put(FPLXDM, invoiceType);
            map.put("cxfs", "1");
            map.put("cxtj", dateS + dateE);
            map.put("cxlx", "0");
        }
        return getJson("FPCX", nsrsbh, TRUE, appid, UUIDUitl.UUID32(), map);

    }

    public static String localYdy(EquipmentEntity equipmentEntity, SynchronizeInvoiceDTO synchronizeInvoiceDTO) {
        Map map = new HashMap();
        String invoiceType = synchronizeInvoiceDTO.getInvoiceType();
        String invoiceCode = synchronizeInvoiceDTO.getInvoiceCode();
        String invoiceNo = synchronizeInvoiceDTO.getInvoiceNo();
        String nsrsbh = equipmentEntity.getSellerTaxNo();
        String appid = equipmentEntity.getAppid();
//        String appid = "d8781634-b06c-4343-bac6-b256435ba68b";
        String mode = equipmentEntity.getEquipmentMode();
        if (mode.equals("0")) {
            map.put(FPLXDM,checkFplxdm(mode,invoiceType));
            map.put("fpdm",invoiceCode);
            map.put("fphm",invoiceNo);
        } else {
            map.put(FPLXDM, invoiceType);
            map.put("fpdm",invoiceCode);
            map.put("fphm",invoiceNo);
        }
        return getJson("LOCALYDY", nsrsbh, TRUE, appid, UUIDUitl.UUID32(), map);
    }


    public static String getInvoicePdf(String invoiceCode, String invoiceNo, String nsrsbh, String appid) {
        Map map = new HashMap();
        List list = new ArrayList();
        Map invoiceMap = new HashMap();
        invoiceMap.put("fpdm", invoiceCode);
        invoiceMap.put("fphm", invoiceNo);
        list.add(invoiceMap);
        map.put("fp", list);
        return getJson("DZFPBSWJCX", nsrsbh, TRUE, appid, UUIDUitl.UUID32(), map);
    }


    public static String getKprq(String kprq) {
        return kprq.substring(0, 6);
    }


    public static String checkFplxdm(String mode, String fplxdm) {
        if (mode.equals("0")) {
            if (fplxdm.equals("004")) {
                fplxdm = "0";
            } else if (fplxdm.equals("007")) {
                fplxdm = "2";
            } else if (fplxdm.equals("026")) {
                fplxdm = "51";
            } else if (fplxdm.equals("005")) {
                fplxdm = "12";
            } else if (fplxdm.equals("12")) {
                fplxdm = "18";
            }
        }
        return fplxdm;
    }
    public static String checkRedFplxdm(String mode, String fplxdm) {
        if (mode.equals("0")) {
            if (fplxdm.equals("004")) {
                fplxdm = "0";
            } else if (fplxdm.equals("007")) {
                fplxdm = "2";
            } else if (fplxdm.equals("026")) {
                fplxdm = "51";
            } else if (fplxdm.equals("005")) {
                fplxdm = "12";
            } else if (fplxdm.equals("12")) {
                fplxdm = "0";
            }
        }else{
            if (fplxdm.equals("12")) {
                fplxdm = "004";
            }
        }
        return fplxdm;
    }

    public static String convertInvoiceTypeJspToSkp(String fplxdm) {
        if (fplxdm.equals("0")) {
            fplxdm = "004";
        } else if (fplxdm.equals("2")) {
            fplxdm = "007";
        } else if (fplxdm.equals("51")) {
            fplxdm = "026";
        }
        return fplxdm;
    }

    public static String getTspz(String mode, String fplxdm) {
        String tspz = "";
        if (mode.equals("0")) {
            if (fplxdm.equals("12")) {
                tspz = "18";
            } else {
                tspz = "0";
            }
        } else if (mode.equals("1")) {
            if (fplxdm.equals("12")) {
                tspz = "12";
            } else {
                tspz = "00";
            }
        } else if (mode.equals("2")) {
            if (fplxdm.equals("12")) {
                tspz = "12";
            } else {
                tspz = "00";
            }
        }
        return tspz;
    }

    /**
     * 空白发票同步
     *
     * @param entity
     * @param fplxdm
     * @return
     */
    public static String blankInvoice(EquipmentEntity entity, String fplxdm) {
        String nsrsbh = entity.getSellerTaxNo();
        String appid = entity.getAppid();
        String equipmentMode = entity.getEquipmentMode();
        Map map = new HashMap();
        if (CodeConstant.COMM_CODE_0.equals(equipmentMode)|| CodeConstant.COMM_CODE_1.equals(equipmentMode)){
            map.put(FPLXDM, "");
        }else if(CodeConstant.COMM_CODE_2.equals(equipmentMode)){
            map.put(FPLXDM, fplxdm);
        }
        return getJson("WLGPCX", nsrsbh, TRUE, appid, UUIDUitl.UUID32(), map);

    }

    /**
     * 余票同步
     *
     * @param entity
     * @param fplxdm
     * @return
     */
    public static String remainderInvoice(EquipmentEntity entity, String fplxdm) {
        String nsrsbh = entity.getSellerTaxNo();
        String appid = entity.getAppid();
        String equipmentMode = entity.getEquipmentMode();
        Map map = new HashMap();
        map.put(FPLXDM, fplxdm);
        return getJson("GPXXCX", nsrsbh, TRUE, appid, UUIDUitl.UUID32(), map);

    }

    /**
     * 余票查询
     *
     * @param appid
     * @param fplxdm
     * @return
     */
    public static String syncRemain(String appid, String nsrsbh, String fplxdm, String equipmentMode) {
        Map map = new HashMap();
        fplxdm = checkFplxdm(equipmentMode, fplxdm);
        map.put(FPLXDM, fplxdm);
        return getJson("HQDQFPDMHM", nsrsbh, TRUE, appid, UUIDUitl.UUID32(), map);
    }

    /**
     * 空白发票作废
     *
     * @param appid
     * @param fplxdm
     * @return
     */
    public static String cancelBlankInvoices(String appid, String nsrsbh, String fplxdm, String equipmentMode,
                                             String invoiceCode, String invoiceNo,String zfr) {
        Map map = new HashMap();
        fplxdm = checkFplxdm(equipmentMode, fplxdm);
        map.put(FPLXDM, fplxdm);
        map.put("fpdm", invoiceCode);
        map.put("fphm", invoiceNo);
        map.put("zfr", zfr);
        return getJson("KBFPZF", nsrsbh, TRUE, appid, UUIDUitl.UUID32(), map);
    }

    /**
     * 监控数据查询（离线金额）/抄报信息同步
     *
     * @param equipmentEntity
     * @param fplxdm
     * @return
     */
    public static String synchronizateRepTax(EquipmentEntity equipmentEntity, String fplxdm) {
        String nsrsbh = equipmentEntity.getSellerTaxNo();
        String appid = equipmentEntity.getAppid();
        String equipmentMode = equipmentEntity.getEquipmentMode();
        Map map = new HashMap();
        if(CodeConstant.JSP.equals(equipmentMode)){
            fplxdm = checkFplxdm(equipmentMode,fplxdm);
        }else if (CodeConstant.SKP.equals(equipmentMode)||CodeConstant.UKEY.equals(equipmentMode)){
            fplxdm = fplxdm;
        }
        map.put(FPLXDM, fplxdm);
        return getJson("JKSJCX", nsrsbh, TRUE, appid, UUIDUitl.UUID32(), map);
    }

    /**
     * 税盘抄报
     *
     * @param equipmentEntity
     * @param fplxdm
     * @return
     */
    public static String sendRepTax(EquipmentEntity equipmentEntity, String nsrsbh, String fplxdm) {
        String equipmentMode = equipmentEntity.getEquipmentMode();
        String appid = equipmentEntity.getAppid();
        if (CodeConstant.JSP.equals(equipmentMode)) {
            fplxdm = "";
        } else if (CodeConstant.SKP.equals(equipmentMode) || CodeConstant.UKEY.equals(equipmentMode)) {
            fplxdm = fplxdm;
        }
        Map map = new HashMap();
        map.put(FPLXDM, fplxdm);
        return getJson("SJCB", nsrsbh, TRUE, appid, UUIDUitl.UUID32(), map);
    }

    /**
     * 税盘清卡
     *
     * @param equipmentEntity
     * @param fplxdm
     * @return
     */
    public static String cleanRepTax(EquipmentEntity equipmentEntity, String nsrsbh, String fplxdm) {
        String equipmentMode = equipmentEntity.getEquipmentMode();
        String appid = equipmentEntity.getAppid();
        if (CodeConstant.JSP.equals(equipmentMode)) {
            fplxdm = "";
        } else if (CodeConstant.SKP.equals(equipmentMode) || CodeConstant.UKEY.equals(equipmentMode)) {
            fplxdm = fplxdm;
        }
        Map map = new HashMap();
        map.put(FPLXDM, fplxdm);
        return getJson("FXJK", nsrsbh, TRUE, appid, UUIDUitl.UUID32(), map);
    }


    /**
     * 同步电票pdf下载地址
     *
     * @param appid
     * @param fps
     * @return
     */

    public static String updatePdfUrl(String appid,String sellerTaxNo,List<Map<String,String>> fps) {
        Map map = new HashMap();
        map.put("fp", fps);
        return getJson("DZFPBSWJCX", sellerTaxNo, TRUE, appid, UUIDUitl.UUID32(), map);
    }

    /**
     * 检测客户端在线状态
     *
     * @param appid
     * @return
     */

    public static String equipmentState(String appid,String sellerTaxNo) {
        Map map = new HashMap();
        return getJson("JCZXZT", sellerTaxNo, TRUE, appid, UUIDUitl.UUID32(), map);
    }

    public static String convertInvoiceTypeName(String InvoiceType) {
        if ("004".equals(InvoiceType)) {
            return "增值税专用发票";
        } else if ("007".equals(InvoiceType)) {
            return "增值税普通发票";
        } else if ("026".equals(InvoiceType)) {
            return "增值税电子普通发票";
        }
        return "";
    }
}
